import Controller from './Controller';

export default class HtmlController extends Controller {

	constructor( parent, object, property, config ) {
		if ( config == null ) config = {};
		super( parent, object, property, 'label', 'div', config );
		this.name( '' );

		this.$html = document.createElement( 'span' );
		this.$html.classList.add( 'lilHtmlSpan' );

		this.$widget.appendChild( this.$html );
		this.$html.innerHTML = object[ property ];

		this.$disable = this.$html;

		this.updateDisplay();

	}

	updateDisplay() {
		this.$html.innerHTML = this.getValue();
		return this;
	}

	/**
	 * 追加到头部
	 * @param value
	 * @param enter
	 * @returns {HtmlController}
	 */
	appendHeadValue( value,enter='\n' ) {
		if ( value == null || value === '' ) return this;

		let valueOri = this.getValue();
		if ( valueOri != null && valueOri !== '' )
			value += enter + valueOri;

		this.setValue( value );
		return this;
	}

	/**
	 * 追加到尾部，需要换行
	 * @param value
	 * @param enter
	 * @returns {HtmlController}
	 */
	appendValue( value,enter='\n') {
		if ( value == null || value === '' ) return this;

		let valueOri = this.getValue();
		if ( valueOri != null && valueOri !== '' )
			value = valueOri + enter + value;

		this.setValue( value );
		return this;
	}

	setStyle( style ){
		if ( style == null || style == '' ) return this;
		if ( this.$html == null ) return this;
		this.$html.setAttribute( 'style', style );
		return this;
	}

	addClass( clazz ){
		if ( clazz == null || clazz == '' ) return this;
		if ( this.$html == null ) return this;
		this.$html.classList.add( clazz );
		return this;
	}

	removeClass( clazz ){
		if ( clazz == null || clazz == '' ) return this;
		if ( this.$html == null ) return this;
		this.$html.classList.remove( clazz );
		return this;
	}

	/**
	 * 不允许换行
	 * @returns {HtmlController}
	 */
	setNowrap(){
		return this.addClass( 'textNowrap' );
	}

}
